{include file="admin@public/header" /}

<link rel="stylesheet" href="/public/static/admin/easyui/layui-v2.6.8/css/layui.css">
<link href="/public/static/admin/blue/bills.css" rel="stylesheet" type="text/css">
  

<div class="wrapper">
    <div class="mod-toolbar-top mr0 cf dn" id="toolTop"></div>
    <div class="bills">
      <div class="con-header" style="padding-right:0px; ">
        <dl class="cf">
          <dd id="identifierType" class="mr20">
            <label>业务类型：</label>
            <span class="ui-combo-wrap" id="transType" style="width: 110px;">
              <input type="text" name="" class="input-txt" autocomplete="off" value="预收冲应收" style="width: 80px;"><i class="trigger"></i>
            </span>
            <!-- 业务类型 -->
            <div class="ui-droplist-wrap" id="transTypeBox" style="position: absolute; top: 70.8906px; z-index: 1000; width: 110px; display: none; left: 114.344px;">
                <div class="droplist" style="position: relative; overflow: auto; min-height: 150px; height: 150px;">
                    <div class="list-item selected" data-value="1" title="预收冲应收">预收冲应收</div>
                    <div class="list-item" data-value="2" title="预付冲应付">预付冲应付</div>
                    <div class="list-item" data-value="3" title="应收冲应付">应收冲应付</div>
                    <div class="list-item" data-value="4" title="应收转应收">应收转应收</div>
                    <div class="list-item" data-value="5" title="应付转应付">应付转应付</div>
                </div>
            </div>
          </dd> 
          <dd class="mr20" id="buIdA">
            <label><span class="red">*</span>客户：</label>
            <span class="ui-combo-wrap" id="customer">
                <input type="text" name="" class="input-txt" autocomplete="off" id="choice_customer_name">
                <i class="ui-icon-ellipsis" onclick="choice_customer('choice_customer')"></i>
            </span>
          </dd>
          <dd class="mr20 tr dn" id="buIdB">
            <label><span class="red">*</span>供应商：</label>
            <span class="ui-combo-wrap" id="supplier">
                <input type="text" name="" class="input-txt" autocomplete="off" id="choice_supplier_name">
                <i class="ui-icon-ellipsis" onclick="choice_supplier('choice_supplier')"></i>
            </span>
          </dd>

          <dd class="mr20 dn" id="buIdC">
            <label><span class="red">*</span>转出客户：</label>
            <span class="ui-combo-wrap" id="customer">
                <input type="text" name="" class="input-txt" autocomplete="off" id="choice_customer_out_name">
                <i class="ui-icon-ellipsis" onclick="choice_customer('choice_customer_out')"></i>
            </span>
          </dd>
          <dd class="mr20 dn" id="buIdD">
            <label><span class="red">*</span>转入客户：</label>
            <span class="ui-combo-wrap" id="customer">
                <input type="text" name="" class="input-txt" autocomplete="off" id="choice_customer_in_name">
                <i class="ui-icon-ellipsis" onclick="choice_customer('choice_customer_in')"></i>
            </span>
          </dd>

          <dd class="mr20 dn" id="buIdE">
            <label><span class="red">*</span>转出供应商：</label>
            <span class="ui-combo-wrap" id="customer">
                <input type="text" name="" class="input-txt" autocomplete="off" id="choice_supplier_out_name">
                <i class="ui-icon-ellipsis" onclick="choice_supplier('choice_supplier_out')"></i>
            </span>
          </dd>
          <dd class="mr20 dn" id="buIdF">
            <label><span class="red">*</span>转入供应商：</label>
            <span class="ui-combo-wrap" id="customer">
                <input type="text" name="" class="input-txt" autocomplete="off" id="choice_supplier_in_name">
                <i class="ui-icon-ellipsis" onclick="choice_supplier('choice_supplier_in')"></i>
            </span>
          </dd>

          <dd class="mr20">
            <label>单据日期：</label>
            <input type="date" id="dj_time" class="ui-input " value="{$today}" style="width: 130px;">
          </dd>
          <dd id="identifier" style="float:right">
            <label>单据编号:</label>
            <span id="number">HXD20210907001</span>
            <i id="editBills"></i>
            <span class="ui-combo-wrap" id="numberAuto" style="display: none;">
              <i class="trigger"></i>
            </span>
          </dd>
        </dl>
        <hr class="hrcls" />
        <div class="cf  ">
          <div class="fl">
            <span  id="config" class="ui-config"><a href="#" class="ui-icon-config-new"></a>列设置</span>
          </div>
          <div class="fr">
            <a class="ui-btn btn-sm" id="sourceA" onclick="sourceA()">选择预收单据</a>
          </div>
        </div>
      </div>

      <!-- 预收单据 -->
      <div class="grid-wrap mb10" id="acGridWrap">
        <table id="accountGrid">
        </table>
      </div>

      <!-- 应收单据 -->
      <div id="standardVersion">
          <div class="tr" style="margin:0 -10px 10px 0; "><a class="ui-btn btn-sm" id="sourceB" onclick="sourceB()">选择应收单据</a></div>
          <div class="grid-wrap" id="billGridWrap">
            <table id="grid">
            </table>
          </div>
      </div>

      <div class="con-footer cf">
        <div class="mb10">
            <textarea type="text" id="note" class="ui-input ui-input-ph" placeholder="暂无备注信息"></textarea>
        </div>
        <div class="cf fr">
          <a href="#" class="linkToCheck ui-btn-bill ui-btn-add mr20">历史单据</a>
          <a href="#" id="operaLog" class="ui-btn fr">操作日志</a>
        </div>
      </div>
      <div class="cf" id="bottomField">
        <span id="groupBtn">
            <a id="savaAndAdd" class="ui-btn ui-btn-sp">保存并新增</a>
            <a id="save" class="ui-btn">保存</a>
            <!-- <a id="shenhe" class="ui-btn">审核</a> -->
        </span>
      </div>
      <div id="mark"></div>
    </div>
    <div id="initCombo" class="dn">
      <input type="text" class="textbox accountAuto" name="account" autocomplete="off">
      <input type="text" class="textbox paymentAuto" name="payment" autocomplete="off">
      <input type="text" class="textbox billAuto" name="goods" autocomplete="off">
    </div>
    <ul id="operaLogBox" class="shadow opera-log-box dn">
      <li>
        <span class="fl" style="margin-right: 50px;"><label>制单人：</label><span id="userName" style="margin-left: 12px;"><span open-data></span></span></span>
      </li>
      <li>
        <span class="fl"><label>制单时间：</label><span id="createTime"></span></span>
      </li>
      <li>
        <span class="fl"><label>最后修改人：</label><span id="modifyName" style="margin-left: 12px;"></span></span>
      </li>
      <li>
        <span class="fl"><label>最后修改时间：</label><span id="modifyTime"></span></span>
      </li>
    </ul>
  </div>

  <div style="display: none;">
    <input type="hidden" id="transType_id" value="1" autocomplete="off" readonly="">
    <input type="hidden" id="choice_customer_id" value="" autocomplete="off" readonly="">
    <input type="hidden" id="choice_supplier_id" value="" autocomplete="off" readonly="">
    <input type="hidden" id="choice_customer_out_id" value="" autocomplete="off" readonly="">
    <input type="hidden" id="choice_customer_in_id" value="" autocomplete="off" readonly="">
    <input type="hidden" id="choice_supplier_out_id" value="" autocomplete="off" readonly="">
    <input type="hidden" id="choice_supplier_in_id" value="" autocomplete="off" readonly="">
  </div>

<script type="text/javascript">


    // 选择源单
    function sourceA(){
      var user_id = $("#choice_customer_id").val();//客户ID
      if (user_id == "") {
          layer.msg("请选择客户");
          return false;
      }

      chooseSourcePayList_order('/admin/basic/choose_source_payment_list.html?user_id='+user_id, this, function (res) {
        //alert("111");
          if (res.btn == 1 && res.status == 1) {
            var data = res.data;
            var source_arr=res.data.info;
            if (data.status == 1) {
              //赋值
              //console.log(source_arr);
              $("#accountGrid").jqGrid("clearGridData");
              for(var i = 0;i<source_arr.length;i++){
                //$("#grid").setGridParam({cellEdit:false});
                var rowData ={
                  'psn': source_arr[i].sn,
                  'type_name': source_arr[i].o_type_name,
                  'dj_time': source_arr[i].dj_time,
                  'jine': source_arr[i].dj_jine,
                  'already_jine': source_arr[i].already_jine,
                  'not_jine': source_arr[i].not_jine,
                  'now_jine': source_arr[i].not_jine,
                  'mark': source_arr[i].mark,
                  'oid':source_arr[i].id,
                  'type': source_arr[i].o_type,
                }
                $("#accountGrid").jqGrid("addRowData", i, rowData);
              }
              //不足4条则补充
              if(4-source_arr.length>0){
                for (var ii = 0; ii < 4-source_arr.length; ii++) {
                      $("#accountGrid").jqGrid('addRowData', source_arr.length+ii+1, { id: ii + 1, oid: 0});
                  }
              }
            } else {
              layer.msg("获取信息失败");
            }
        }
      });
    }

</script>


<script type="text/javascript">


    // 选择源单
    function sourceB(){
      var user_id = $("#choice_customer_id").val();//客户ID
      if (user_id == "") {
          layer.msg("请选择客户");
          return false;
      }

      chooseSource_order('/admin/basic/choose_source_list.html?user_id='+user_id, this, function (res) {
          if (res.btn == 1 && res.status == 1) {
            var data = res.data;
            var source_arr=res.data.info;
            if (data.status == 1) {
              //赋值
              //console.log(source_arr);
              $("#grid").jqGrid("clearGridData");
              for(var i = 0;i<source_arr.length;i++){
                //$("#grid").setGridParam({cellEdit:false});
                var rowData ={
                  'psn': source_arr[i].sn,
                  'type_name': source_arr[i].o_type_name,
                  'dj_time': source_arr[i].dj_time,
                  'jine': source_arr[i].dj_jine,
                  'already_jine': source_arr[i].already_jine,
                  'not_jine': source_arr[i].not_jine,
                  'now_jine': source_arr[i].dj_jine,
                  'mark': source_arr[i].mark,
                  'oid':source_arr[i].id,
                  'type': source_arr[i].o_type,
                }
                $("#grid").jqGrid("addRowData", i, rowData);

                //$("#grid").jqGrid('setCell', i, 'now_jine', '', 'edit-cell');//设置单元格可以编辑
                //$("#grid").jqGrid().trigger("reloadGrid");
              }
              //不足5条则补充
              if(5-source_arr.length>0){
                for (var ii = 0; ii < 5-source_arr.length; ii++) {
                      $("#grid").jqGrid('addRowData', source_arr.length+ii+1, { id: ii + 1, oid: 0});
                  }
              }
            } else {
              layer.msg("获取信息失败");
            }
        }
      });
    }

</script>

<script>
  
    $(document).ready(function () {


      // 选择业务类型-弹窗
      $("#identifierType").on("click", "#transType", function (e) {
          var top = $(this).offset().top;
          var left = $(this).offset().left;
          $("#transTypeBox").toggle();
          $("#transTypeBox").css({ "top": (top + 10) + "px", "left": (left - 20) + "px" });
      })
      //选择业务类型-赋值
      $("#transTypeBox .droplist").on("click", ".list-item", function (e) {
          var title = $(this).attr("title");
          var val=$(this).attr("data-value");
          $("#transType_id").val(val);
          $("#transType>input").val(title);
          $(this).addClass("selected").siblings().removeClass("selected");
          $("#transTypeBox").hide();

          if(val==1){
            $("#buIdA").removeClass("dn");
            $("#buIdB").addClass("dn");
            $("#buIdC").addClass("dn");
            $("#buIdD").addClass("dn");
            $("#buIdE").addClass("dn");
            $("#buIdF").addClass("dn");
          }else if(val==2){
            $("#buIdA").addClass("dn");
            $("#buIdB").removeClass("dn");
            $("#buIdC").addClass("dn");
            $("#buIdD").addClass("dn");
            $("#buIdE").addClass("dn");
            $("#buIdF").addClass("dn");
          }else if(val==3){
            $("#buIdA").removeClass("dn");
            $("#buIdB").removeClass("dn");
            $("#buIdC").addClass("dn");
            $("#buIdD").addClass("dn");
            $("#buIdE").addClass("dn");
            $("#buIdF").addClass("dn");
          }else if(val==4){
            $("#buIdA").addClass("dn");
            $("#buIdB").addClass("dn");
            $("#buIdC").removeClass("dn");
            $("#buIdD").removeClass("dn");
            $("#buIdE").addClass("dn");
            $("#buIdF").addClass("dn");
          }else if(val==5){
            $("#buIdA").addClass("dn");
            $("#buIdB").addClass("dn");
            $("#buIdC").addClass("dn");
            $("#buIdD").addClass("dn");
            $("#buIdE").removeClass("dn");
            $("#buIdF").removeClass("dn");
          }

      });
    })

    //选择业务类型-选择框外点击则隐藏
    $(document).mouseup(function (e) {
        if(e.target.id!="identifierType"){
            var con = $("#transTypeBox");   // 设置目标区域
            if (!con.is(e.target) && con.has(e.target).length === 0) {
                 $("#transTypeBox").hide();
            }
        } 
    });
</script>
<script>
    var edit_row = 0;
    var edit_col = 0;
    var row_id = 0;
    var col_name = 0;

    $(document).ready(function () {

        $("#accountGrid").jqGrid({
            // url: '/admin/store/index.html?ajax=100',
            datatype: "local",//数据来源，本地数据（local，json,jsonp,xml等）
            height: '90%',//高度，表格高度。可为数值、百分比或'auto'
 
 			      colNames: ['源单编号','业务类别', '单据日期', '单据金额', '已核销金额', '未核销金额', '<span class="red">*</span>本次核销金额','ID','TYPE'],
            colModel: [
               
                { name: 'psn', width: 150},
                { name: 'type_name', width: 100},
                { name: 'dj_time', width: 100},
                { name: 'jine', width: 100, align:'right'},
                { name: 'already_jine', width: 100, align:'right'},
                { name: 'not_jine', width: 100, align:'right'},
                { name: 'now_jine', width: 100,editable: true, align:'right', editoptions: {
                    size: 25,
                    dataEvents: [
                      {
                        type: 'change',     //blur,focus,change............. 
                        fn: function (e) {
                          reload_val();
                        }
                      }
                    ]
                  }
                },
                { name: 'oid', hidden:true},
                { name: 'type', hidden:true},
            ],
            cellEdit: true,
            cellurl: '/',
            rowNum: 10,
            rowList: [10, 20, 30],
            sortname: 'id',
            sortorder: "desc",
            pagerpos: "left",//分页位置
            pgbuttons: true,//翻页按钮
            pagination: true,
            altRows: !0,
            gridview: !0,
            shrinkToFit: !1,
            cellLayout: 8,
            autowidth: !0,
            pager: "#page",
            viewrecords: !0,
            rownumbers: true, //自增序号
            cmTemplate: {
                sortable: !1,
                title: !1
            },
            loadError: function (t, e, i) {
                parent.Public.tips({
                    type: 1,
                    content: "操作失败了哦，请检查您的网络链接！"
                })
            },
            beforeEditCell: function (rowid, cellname, value, iRow, iCol) {
                col_name = cellname;
                edit_row = iRow;
                edit_col = iCol;
                row_id = rowid;
            },
            // 合计
            footerrow: true,
            gridComplete: function () {
              // 合计的变量
              var jines = $("#accountGrid").getCol('jine', false, 'sum');
              var already_jines = $("#accountGrid").getCol('already_jine', false, 'sum');
              var not_jines = $("#accountGrid").getCol('not_jine', false, 'sum');
              var now_jines = $("#accountGrid").getCol('now_jine', false, 'sum');
              $("#hexiao_jine").val(now_jines);//核销总金额
         
              
              var user_arrears= $('#real_qk').val();//真实总欠款
              var zk_jine = $('#zk_jine').val();//整单折扣
              var jine= $("#jine").val();//付款总金额
              $('#cha_jine').val(parseFloat(user_arrears)-parseFloat(not_jines)-parseFloat(zk_jine)-parseFloat(jine));//总欠款=真实总欠款-折扣-付款总金额-未核销总金额
              $("#now_jine").val(parseFloat(not_jines)-parseFloat(now_jines)+parseFloat(zk_jine)+parseFloat(jine));//本次预收款=未核销-本次核销+折扣+付款总金额
              $("#accountGrid").footerData('set', { "psn": '合计：', jine: jines,already_jine:already_jines,not_jine:not_jines,now_jine:now_jines});
           
            },
            resizeStart:function(){
              console.log("aa");
              $("#rs_maccountGrid").css({"height":"0 !important"});
            },
            resizeStop:function(){
              console.log("bb");
              $("#rs_maccountGrid").css({"height":"0 !important"});
            }

        })
        var mydata = [];
        for (var i = 0; i < 4; i++) {
            var obj = { id: i + 1, goodsname: "", dw: "" ,good_id:"0",mark:""}
            mydata.push(obj);
        }
        for (var i = 0; i <= mydata.length; i++) {
            jQuery("#accountGrid").jqGrid('addRowData', i + 1, mydata[i]);
        }

    })

   

</script>

<script>
    var edit_row1 = 0;
    var edit_col1 = 0;
    var row_id1 = 0;
    var col_name1 = 0;

    $(document).ready(function () {

        $("#grid").jqGrid({
            // url: '/admin/store/index.html?ajax=100',
            datatype: "local",//数据来源，本地数据（local，json,jsonp,xml等）
            height: '90%',//高度，表格高度。可为数值、百分比或'auto'
 
 			colNames: ['源单编号','业务类别', '单据日期', '单据金额', '已核销金额', '未核销金额', '<span class="red">*</span>本次核销金额','ID','TYPE'],
            colModel: [
              
                { name: 'psn', width: 150},
                { name: 'type_name', width: 100},
                { name: 'dj_time', width: 100},
                { name: 'jine', width: 100, align:'right'},
                { name: 'already_jine', width: 100, align:'right'},
                { name: 'not_jine', width: 100, align:'right'},
                { name: 'now_jine', width: 100,editable: true, align:'right', editoptions: {
                    size: 25,
                    dataEvents: [
                      {
                        type: 'change',     //blur,focus,change............. 
                        fn: function (e) {
                          reload_val1();
                        }
                      }
                    ]
                  }
                },
                { name: 'oid', hidden:true},
                { name: 'type', hidden:true},
            ],
            cellEdit: true,
            cellurl: '/',
            rowNum: 10,
            rowList: [10, 20, 30],
            sortname: 'id',
            sortorder: "desc",
            pagerpos: "left",//分页位置
            pgbuttons: true,//翻页按钮
            pagination: true,
            altRows: !0,
            gridview: !0,
            shrinkToFit: !1,
            cellLayout: 8,
            autowidth: !0,
            pager: "#page",
            viewrecords: !0,
            rownumbers: true, //自增序号
            cmTemplate: {
                sortable: !1,
                title: !1
            },
            loadError: function (t, e, i) {
                parent.Public.tips({
                    type: 1,
                    content: "操作失败了哦，请检查您的网络链接！"
                })
            },
            beforeEditCell: function (rowid, cellname, value, iRow, iCol) {
                col_name1 = cellname;
                edit_row1 = iRow;
                edit_col1 = iCol;
                row_id1 = rowid;
            },
            // 合计
            footerrow: true,
            gridComplete: function () {
              // 合计的变量
              var jines = $("#grid").getCol('jine', false, 'sum');
              var already_jines = $("#grid").getCol('already_jine', false, 'sum');
              var not_jines = $("#grid").getCol('not_jine', false, 'sum');
              var now_jines = $("#grid").getCol('now_jine', false, 'sum');
              $("#hexiao_jine1").val(now_jines);//核销总金额
         
              
              var user_arrears= $('#real_qk').val();//真实总欠款
              var zk_jine = $('#zk_jine').val();//整单折扣
              var jine= $("#jine").val();//付款总金额
              $('#cha_jine').val(parseFloat(user_arrears)-parseFloat(not_jines)-parseFloat(zk_jine)-parseFloat(jine));//总欠款=真实总欠款-折扣-付款总金额-未核销总金额
              $("#now_jine").val(parseFloat(not_jines)-parseFloat(now_jines)+parseFloat(zk_jine)+parseFloat(jine));//本次预收款=未核销-本次核销+折扣+付款总金额
              $("#grid").footerData('set', { "psn": '合计：', jine: jines,already_jine:already_jines,not_jine:not_jines,now_jine:now_jines});
           
            },

        })
        var mydata = [];
        for (var i = 0; i < 4; i++) {
            var obj = { id: i + 1, goodsname: "", dw: "" ,good_id:"0",mark:""}
            mydata.push(obj);
        }
        for (var i = 0; i <= mydata.length; i++) {
            jQuery("#grid").jqGrid('addRowData', i + 1, mydata[i]);
        }

    })

   

</script>

<script>
    // 监听“本次核销金额”的改变
    function reload_val() {
        var rid = row_id;
        var c_name = col_name;
        if (c_name == "now_jine") {
            // 合计的变量
            var jines =  parseFloat($("#accountGrid").getCol('jine', false, 'sum')).toFixed(2);
            var already_jines =  parseFloat($("#accountGrid").getCol('already_jine', false, 'sum')).toFixed(2);
            var not_jines =  parseFloat($("#accountGrid").getCol('not_jine', false, 'sum')).toFixed(2);
            var now_jines = parseFloat($("#accountGrid").getCol('now_jine', false, 'sum')).toFixed(2);
    
            $("#hexiao_jine").val(now_jines);//核销总金额
            var user_arrears= $('#real_qk').val();//真实总欠款
            var zk_jine = $('#zk_jine').val();//整单折扣
            var jine= $("#jine").val();//付款总金额

            $('#cha_jine').val(parseFloat(user_arrears)-parseFloat(not_jines)-parseFloat(zk_jine)-parseFloat(jine));//总欠款=真实总欠款-折扣-付款总金额-未核销总金额
            $("#now_jine").val(parseFloat(not_jines)-parseFloat(now_jines)+parseFloat(zk_jine)+parseFloat(jine));//本次预收款=未核销-本次核销+折扣+付款总金额
            $("#accountGrid").footerData('set', { "psn": '合计：', jine: jines,already_jine:already_jines,not_jine:not_jines,now_jine:now_jines});
        } 


    }

    // // 监听“本次核销金额”的改变
    function reload_val1() {
        var rid = row_id1;
        var c_name = col_name1;
        if (c_name == "now_jine") {
            // 合计的变量
            var jines =  parseFloat($("#grid").getCol('jine', false, 'sum')).toFixed(2);
            var already_jines =  parseFloat($("#grid").getCol('already_jine', false, 'sum')).toFixed(2);
            var not_jines =  parseFloat($("#grid").getCol('not_jine', false, 'sum')).toFixed(2);
            var now_jines = parseFloat($("#grid").getCol('now_jine', false, 'sum')).toFixed(2);
    
            $("#hexiao_jine1").val(now_jines);//核销总金额
            var user_arrears= $('#real_qk').val();//真实总欠款
            var zk_jine = $('#zk_jine').val();//整单折扣
            var jine= $("#jine").val();//付款总金额

            $('#cha_jine').val(parseFloat(user_arrears)-parseFloat(not_jines)-parseFloat(zk_jine)-parseFloat(jine));//总欠款=真实总欠款-折扣-付款总金额-未核销总金额
            $("#now_jine").val(parseFloat(not_jines)-parseFloat(now_jines)+parseFloat(zk_jine)+parseFloat(jine));//本次预收款=未核销-本次核销+折扣+付款总金额
            $("#grid").footerData('set', { "psn": '合计：', jine: jines,already_jine:already_jines,not_jine:not_jines,now_jine:now_jines});
        } 


    }

    // 选择客户

     // 选择客户
    function choice_customer(id) {
      layer.open({
        type: 2,
        title: "选择客户",
        shadeClose: true,
        shade: 0.3,
        maxmin: false, //开启最大化最小化按钮
        area: ['555px', '490px'],
        fixed: false, //不固定
        btn: ['确定', '取消'],
        content: "/admin/basic/choose_customer.html?single=1&is_customer=1",//单选，客户
        btn1: function (index, layero) {
          var body = layer.getChildFrame('body', index);
          //获取所有选中行
          //var ids =  body.find("#grid").jqGrid('getGridParam','selarrrow');
          var ids =  body.find("#grid").jqGrid('getGridParam','selrow');

          if (ids == '' || ids == null) {
            layer.msg("请选择客户");
            return false;
          } else {
            //获取客户信息
            $.get("/admin/basic/one_customer.html", { id: ids }, function (data, state) {
              // console.log("111");
              //  console.log(id);
              //   console.log(data);
              //    console.log(data.info.id);
              if (data.status == 1) {
                $("#"+id+"_name").val(data.info.sn + " " + data.info.name);
                $("#"+id+"_id").val(data.info.id);
              } else {
                layer.msg("获取信息失败");
              }
            })
          }
          layer.close(index);
          return false;
        },
        btn4: function (index, layero) {
          layer.close(index);
          return false;
        }
      })
    }

    // 选择供应商
    function choice_supplier(id) {
      layer.open({
        type: 2,
        title: "选择供应商",
        shadeClose: true,
        shade: 0.3,
        maxmin: false, //开启最大化最小化按钮
        area: ['600px', '510px'],
        fixed: false, //不固定
        btn: ['确定', '取消'],
        content: "/admin/basic/choose_customer.html?single=1&is_customer=0",//单选，供应商
        btn1: function (index, layero) {
          var body = layer.getChildFrame('body', index);
          //获取所有选中行
          //var ids =  body.find("#grid").jqGrid('getGridParam','selarrrow');
          var ids =  body.find("#grid").jqGrid('getGridParam','selrow');

          if (ids == '' || ids == null) {
            layer.msg("选择供应商");
            return false;
          } else {
            //alert(ids);
            //return false;
            //获取商品信息
            $.get("/admin/basic/one_customer.html", { ids: ids }, function (data, state) {
              if (data.status == 1) {
                $("#"+id+"_name").val(data.info.names);
                $("#"+id+"_id").val(data.info.ids);
              } else {
                layer.msg("获取信息失败");
              }
            })
          }
          layer.close(index);
          return false;
        },
        btn4: function (index, layero) {
          layer.close(index);
          return false;
        }
      })
    }
</script>

<script type="text/javascript">
  var grid = $('#grid');
  var accountGrid=$('#accountGrid');
  $(".wrapper").on("click", "#savaAndAdd", function (e) {
    console.log("111");
    //先保存
    save_jqgrid(edit_row,edit_col,accountGrid,function (res1) {
      save_jqgrid(edit_row1,edit_col1,grid,function (res) {
        savedata();
      });
    });
  })



  $(".wrapper").on("click", "#save", function (e) {
    save_jqgrid(edit_row,edit_col,accountGrid,function(res1){
      save_jqgrid(edit_row1,edit_col1,grid,function(res){
         $("#accountGrid").jqGrid().trigger("reloadGrid");
          $("#grid").jqGrid().trigger("reloadGrid");
        layer.msg("保存成功");
      });
    });
  })

  function savedata() {
    var gooddata = $('#grid').jqGrid('getGridParam', 'data');
    var accountdata = $('#accountGrid').jqGrid('getGridParam', 'data');

    var now_jines1 = $("#grid").getCol('now_jine', false, 'sum');//核销总金额
    var now_jines2 = $("#accountGrid").getCol('now_jine', false, 'sum');//核销总金额
    // console.log(now_jines1);
    // console.log(now_jines2);

    if (now_jines1==0||now_jines2==0) {
       layer.msg("请选择核销订单");
       return false;
    }
    if (now_jines1 != now_jines2) {
       layer.msg("核销金额合计要相等");
       return false;
    }

    var user_id = $("#choice_customer_id").val();//客户ID
    if (user_id == "") {
      layer.msg("请选择用户");
      return false;
    }

    var flag = 0;
  
    accountdata.forEach(function (self, index) {
 
      if (self['now_jine'] > self['not_jine'] ) {
        flag = 1;
      }
    });
  
    gooddata.forEach(function (self, index) {
 
      if (self['now_jine'] > self['not_jine'] ) {
        flag = 1;
      }
    });
    
    if (flag == 1) {
      layer.msg("核销金额不能大于未核销金额");
      return false;
    }



    var dj_time = $("#dj_time").val();//单据日期
    var sn = $("#number").html();//单据号
    var mark = $("#note").val();//备注
    var transType_id = $("#transType_id").val();//业务类型id
    $.ajax({
      type: 'POST',
      url: "save.html",
      data: { user_id: user_id, sn: sn, mark: mark, gooddata: gooddata,accountdata:accountdata, dj_time: dj_time,type:transType_id,hexiao_jine:now_jines1},
      dataType: "json",
      async: false,
      error: function (request) {
        layer.msg("发送请求失败！");
      },
      success: function (data) {
        if (data.status == 1) {
          layer.msg(data.msg);
        } else {
          layer.msg(data.msg);
        }

      }
    });

  }


</script>


{include file="admin@public/footer" /}